package com.zy.swagger.controller.sys;

import com.zy.swagger.service.sys.DingTalkService;
import com.zy.swagger.util.ExportExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * description 考勤控制器
 *
 * @author ZY
 * @date 2021年04月04日 13:17
 **/
@Api(tags = "考勤管理")
@Slf4j
@RestController
@RequestMapping("/attend")
public class AttendanceController {

    @Resource
    private DingTalkService dingTalkService;

    @ApiOperation("导出考勤")
    @GetMapping("/downloadOvertimeAttend")
    public void downloadOvertimeAttend(String deptName, String deptId, String year, String month, HttpServletResponse response) throws Exception {
        log.info("下载加班考勤入参：deptName={}, deptId={}，year={}，month={}", deptName, deptId, year, month);
        String title = deptName + year + "年" + month + "月餐补统计";
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(title, "UTF-8") + ".xlsx");
        response.setHeader("Connection", "close");
        OutputStream outputStream = response.getOutputStream();
        ExportExcelUtil eeu = new ExportExcelUtil();
        XSSFWorkbook workbook = new XSSFWorkbook();
        String[] header = {"姓名", "日期", "下班时间", "补贴类型", "金额(元)"};
        Map<String, List<List<String>>> map = dingTalkService.getOverTimeAttendList(deptName, Integer.parseInt(deptId), Integer.parseInt(year), Integer.parseInt(month));
        int i = 0;
        for (String name : map.keySet()) {
            eeu.exportExcel(workbook, i++, name, header, map.get(name));
        }
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }

}
